package cn.ynrcb.personnelfile.admin.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.ynrcb.personnelfile.admin.config.SmsConfig;
import cn.ynrcb.personnelfile.admin.entity.SmsLogEntity;
import cn.ynrcb.personnelfile.admin.entity.SmsResponseResult;
import cn.ynrcb.personnelfile.admin.enums.SmsStatusEnum;
import cn.ynrcb.personnelfile.admin.enums.SmsTypeEnum;
import cn.ynrcb.personnelfile.admin.service.SmsLogService;
import cn.ynrcb.personnelfile.admin.service.SmsService;
import cn.ynrcb.personnelfile.admin.vo.SmsVo;
import cn.ynrcb.personnelfile.common.utils.DateUtils;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.common.utils.SmsSendClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service("smsService")
public class SmsServiceImpl implements SmsService {


    @Autowired
    private SmsLogService smsLogService;

    @Override
    public R sendSms(SmsVo.SendSmsParm param) {

        String phone = param.getPhone();

        SmsLogEntity entity = smsLogService.findSmsLog(phone, param.getType().getCode(), SmsStatusEnum.SUCC.getCode());
        if (entity != null) {

            //上次短信发送时间+间隔时间
            Date interval = DateUtils.addDateSeconds(entity.getCreateTime(), SmsConfig.smsInterval);
            if (new Date().before(interval)) {
                return R.error("操作频繁");
            }
        }

        String smsCode = RandomUtil.randomNumbers(6);

        SmsLogEntity smsLog = new SmsLogEntity();
        smsLog.setPhone(phone);
        smsLog.setCode(smsCode);
        smsLog.setType(param.getType().getCode());
        smsLog.setStatus(SmsStatusEnum.NONE.getCode());

        try {
            String ret = new SmsSendClient().sendSmsCode(smsLog);
            smsLog.setResponseData(ret);
            log.info("手机号:" + phone + "，发送验证码返回结果：" + ret);

            if (ret.indexOf("-") > -1) {
                smsLog.setErrorReason(ret);
                smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
            } else {
                smsLog.setStatus(SmsStatusEnum.SUCC.getCode());
            }

        } catch (Exception e) {
            smsLog.setErrorReason(e.getMessage());
            smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
        }

        Date date = new Date();
        smsLog.setExpireTime(DateUtils.addDateSeconds(date, SmsConfig.smsExpire));

        smsLogService.save(smsLog);
        if (smsLog.getStatus() == SmsStatusEnum.SUCC.getCode()) {
            return R.ok("验证码发送成功！");
        } else {
            return R.error("验证码发送失败！");
        }
    }

    @Override
    public boolean checkSmsCode(SmsVo.CheckSmsParm param) {

        SmsLogEntity entity = smsLogService.findSmsLog(param.getPhone(), param.getType().getCode(), SmsStatusEnum.SUCC.getCode());
        //验证码不存在
        if (entity == null) {
            return false;
        }

        //验证码已过期
        if (new Date().after(entity.getExpireTime())) {
            return false;
        }

        //错误
        if (!param.getSmsCode().equals(entity.getCode())) {
            return false;
        }

        return true;
    }

    @Override
    public Boolean sendAuditSms(String mobile, String auditRemark) {
        SmsLogEntity smsLog = new SmsLogEntity();
        smsLog.setPhone(mobile);
        smsLog.setContent(auditRemark);
        smsLog.setType(SmsTypeEnum.AUDIT_RESULT.getCode());
        smsLog.setCreateTime(new Date());
        SmsResponseResult smsResponseResult = null;
        try {
            String ret = new SmsSendClient().sendSms(smsLog);
            smsLog.setResponseData(ret);

            if (ret.indexOf("-") > -1) {
                smsLog.setErrorReason(ret);
                smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
            } else {
                smsLog.setStatus(SmsStatusEnum.SUCC.getCode());
            }

        } catch (Exception e) {
            smsLog.setErrorReason(e.getMessage());
            smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
        }

        smsLogService.save(smsLog);
        if (smsLog.getStatus() == SmsStatusEnum.SUCC.getCode()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean sendComplaint(String phone, String content) {
        SmsLogEntity smsLog = new SmsLogEntity();
        smsLog.setPhone(phone);
        smsLog.setContent(content);
        smsLog.setType(SmsTypeEnum.COMPLAINT_TIP.getCode());
        smsLog.setCreateTime(new Date());
        SmsResponseResult smsResponseResult = null;
        try {
            String ret = new SmsSendClient().sendSms(smsLog);
            smsLog.setResponseData(ret);

            if (ret.indexOf("-") > -1) {
                smsLog.setErrorReason(ret);
                smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
            } else {
                smsLog.setStatus(SmsStatusEnum.SUCC.getCode());
            }

        } catch (Exception e) {
            smsLog.setErrorReason(e.getMessage());
            smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
        }

        smsLogService.save(smsLog);
        if (smsLog.getStatus() == SmsStatusEnum.SUCC.getCode()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean sendH5Audit(String phone, String content) {
        SmsLogEntity smsLog = new SmsLogEntity();
        smsLog.setPhone(phone);
        smsLog.setContent(content);
        smsLog.setType(SmsTypeEnum.ZZBA_TOKEN.getCode());
        smsLog.setCreateTime(new Date());
        SmsResponseResult smsResponseResult = null;
        try {
            String ret = new SmsSendClient().sendH5Msg(smsLog);
            smsLog.setResponseData(ret);

            if (ret.indexOf("-") > -1) {
                smsLog.setErrorReason(ret);
                smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
            } else {
                smsLog.setStatus(SmsStatusEnum.SUCC.getCode());
            }

        } catch (Exception e) {
            smsLog.setErrorReason(e.getMessage());
            smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
        }

        smsLogService.save(smsLog);
        if (smsLog.getStatus() == SmsStatusEnum.SUCC.getCode()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public R sendMsg(SmsVo.SendSmsParm params) {
        String phone = params.getPhone();

        SmsLogEntity entity = smsLogService.findSmsLog(phone, params.getType().getCode(), SmsStatusEnum.SUCC.getCode());
        if (entity != null) {

            //上次短信发送时间+间隔时间
            Date interval = DateUtils.addDateSeconds(entity.getCreateTime(), SmsConfig.smsInterval);
            if (new Date().before(interval)) {
                return R.error("操作频繁");
            }
        }

        String smsCode = RandomUtil.randomNumbers(6);

        SmsLogEntity smsLog = new SmsLogEntity();
        smsLog.setPhone(phone);
        smsLog.setCode(smsCode);
        smsLog.setType(params.getType().getCode());
        smsLog.setStatus(SmsStatusEnum.NONE.getCode());

        try {
            String ret = new SmsSendClient().sendMsg(smsLog);
            smsLog.setResponseData(ret);
            log.info("手机号:" + phone + "，发送验证码返回结果：" + ret);

            if (ret.indexOf("-") > -1) {
                smsLog.setErrorReason(ret);
                smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
            } else {
                smsLog.setStatus(SmsStatusEnum.SUCC.getCode());
            }

        } catch (Exception e) {
            smsLog.setErrorReason(e.getMessage());
            smsLog.setStatus(SmsStatusEnum.FAIL.getCode());
        }

        Date date = new Date();
        smsLog.setExpireTime(DateUtils.addDateSeconds(date, SmsConfig.smsExpire));

        smsLogService.save(smsLog);
        if (smsLog.getStatus() == SmsStatusEnum.SUCC.getCode()) {
            return R.ok("验证码发送成功！");
        } else {
            return R.error("验证码发送失败！");
        }
    }

}
